package com.jackrain.nea.wms.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ReferenceUtil;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.wms.api.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;

/**
 *
 *
 * @author xuixnhsui
 * @date 2018/05/09
 **/
@Slf4j
@Api(description = "WMS中间表新增")
@RestController
public class BaseWmsCtrl {

    @Reference(version = "1.0", group = "wms")
    private BaseWmsSaveSuplierCmd baseWmsSaveSuplierCmd;

    @Reference(version = "1.0", group = "wms")
    private BaseWmsSaveCmd baseWmsSaveCmd;

    @Reference(version = "1.0", group = "wms")
    private BaseWmsDelCmd baseWmsDelCmd;

    @Reference(version = "1.0", group = "wms")
    private WmsModifySaveCmd wmsModifySaveCmd;

    @ApiOperation(value = "WMS中间表新增")
    @RequestMapping(path = "/wms/save", method = RequestMethod.POST)
    public JSONObject wmssavePost(HttpServletRequest request,
                                     @RequestParam(value = "param", required = true) String param) throws Exception {

        ValueHolder result = new ValueHolder();

        try {
            HashMap map = new HashMap(1);
            map.put("param", JSON.parseObject(param));
            Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.wms.api.BaseWmsSaveCmd", "wms", "1.0");
            result = ((BaseWmsSaveCmd) o).execute(map);
            if (result == null){
                result = new ValueHolder();
                result.put("code", -1);
                result.put("error", "服务错误：返回为空");
            }
            if (result.get("code") == null){
                result.put("code", 0);
            }

        } catch (Exception e) {
            result.put("code", -1);
            if (e instanceof NDSException){
                result.put("error", "服务错误：" + e.getMessage());
            }else{
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                e.printStackTrace(pw);
                result.put("error", "服务错误：" + sw.toString());
            }
        }

        return result.toJSONObject();

    }

    @ApiOperation(value = "改单通知WMS中间表新增")
    @RequestMapping(path = "/wms/save/modify", method = RequestMethod.POST)
    public JSONObject wmssaveModify(HttpServletRequest request,
                                  @RequestParam(value = "param", required = true) String param) throws Exception {

        ValueHolder result = new ValueHolder();

        try {
            HashMap map = new HashMap(1);
            map.put("param", JSON.parseObject(param));
            Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.wms.api.WmsModifySaveCmd", "wms", "1.0");
            result = ((WmsModifySaveCmd) o).execute(map);
            if (result == null){
                result = new ValueHolder();
                result.put("code", -1);
                result.put("error", "服务错误：返回为空");
            }
            if (result.get("code") == null){
                result.put("code", 0);
            }

        } catch (Exception e) {
            result.put("code", -1);
            if (e instanceof NDSException){
                result.put("error", "服务错误：" + e.getMessage());
            }else{
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                e.printStackTrace(pw);
                result.put("error", "服务错误：" + sw.toString());
            }
        }

        return result.toJSONObject();

    }
    @ApiOperation(value = "商品供应关系新增")
    @RequestMapping(path = "/wms/save/supplierincr", method = RequestMethod.POST)
    public JSONObject wmssavesuplierincrPost(HttpServletRequest request,
                                         @RequestParam(value = "param", required = true) String param) throws Exception {

        ValueHolder result = new ValueHolder();
        try {
            HashMap map = new HashMap(1);
            map.put("param", JSON.parseObject(param));
            Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.wms.api.BaseWmsSaveSuplierIncrCmd", "wms", "1.0");
            result = ((BaseWmsSaveSuplierIncrCmd) o).execute(map);
            if (result == null){
                result = new ValueHolder();
                result.put("code", -1);
                result.put("error", "服务错误：返回为空");
            }
            if (result.get("code") == null){
                result.put("code", 0);
            }

        } catch (Exception e) {
            result.put("code", -1);
            if (e instanceof NDSException){
                result.put("error", "服务错误：" + e.getMessage());
            }else{
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                e.printStackTrace(pw);
                result.put("error", "服务错误：" + sw.toString());
            }
        }

        return result.toJSONObject();

    }
    @ApiOperation(value = "商品供应关系批量新增")
    @RequestMapping(path = "/wms/save/supplier", method = RequestMethod.POST)
    public JSONObject wmssavesuplierPost(HttpServletRequest request,
                                  @RequestParam(value = "param", required = true) String param) throws Exception {

        ValueHolder result = new ValueHolder();
        try {
            HashMap map = new HashMap(1);
            map.put("param", JSON.parseObject(param));
            Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.wms.api.BaseWmsSaveSuplierCmd", "wms", "1.0");
            result = ((BaseWmsSaveSuplierCmd) o).execute(map);
            if (result == null){
                result = new ValueHolder();
                result.put("code", -1);
                result.put("error", "服务错误：返回为空");
            }
            if (result.get("code") == null){
                result.put("code", 0);
            }

        } catch (Exception e) {
            result.put("code", -1);
            if (e instanceof NDSException){
                result.put("error", "服务错误：" + e.getMessage());
            }else{
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                e.printStackTrace(pw);
                result.put("error", "服务错误：" + sw.toString());
            }
        }

        return result.toJSONObject();

    }
    @ApiOperation(value = "WMS中间表新增")
    @RequestMapping(path = "/wms/delete", method = RequestMethod.POST)
    public JSONObject wmsDelPost(HttpServletRequest request,
                                  @RequestParam(value = "param", required = true) String param) throws Exception {

        ValueHolder result = new ValueHolder();
        try {
            HashMap map = new HashMap(1);
            map.put("param", JSON.parseObject(param));
            Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.wms.api.BaseWmsDelCmd", "wms", "1.0");
            result = ((BaseWmsDelCmd) o).execute(map);
            if (result == null){
                result = new ValueHolder();
                result.put("code", -1);
                result.put("error", "服务错误：返回为空");
            }
            if (result.get("code") == null){
                result.put("code", 0);
            }

        } catch (Exception e) {
            result.put("code", -1);
            if (e instanceof NDSException){
                result.put("error", "服务错误：" + e.getMessage());
            }else{
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                e.printStackTrace(pw);
                result.put("error", "服务错误：" + sw.toString());
            }
        }

        return result.toJSONObject();

    }

}
